<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="common/include :: header('服务单项列表')" />
</head>
<body class="gray-bg">
	<div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-collapse">
				<form id="post-form">
					<div class="select-list">
						<ul>
							<li>
								服务单项名称：<input type="text" name="name"/>
							</li>
							<li>
								是否套餐：
								<select name="carPackage" th:with="type=${@dict.getType('si_car_package')}">
									<option value="">所有</option>
									<option th:each="dict : ${type}" th:text="${dict.label}" th:value="${dict.value}"></option>
								</select>
							</li>
							<li>
								服务分类：
								<select name="serviceCatalog" th:with="type=${@dict.getType('si_service_catalog')}">
									<option value="">所有</option>
									<option th:each="dict : ${type}" th:text="${dict.label}" th:value="${dict.value}"></option>
								</select>
							</li>
							<li>
								审核状态：
								<select name="auditStatus" th:with="type=${@dict.getType('si_audit_status')}">
									<option value="">所有</option>
									<option th:each="dict : ${type}" th:text="${dict.label}" th:value="${dict.value}"></option>
								</select>
							</li>
							<li>
								上架状态：
								<select name="saleStatus" th:with="type=${@dict.getType('si_sale_status')}">
									<option value="">所有</option>
									<option th:each="dict : ${type}" th:text="${dict.label}" th:value="${dict.value}"></option>
								</select>
							</li>
							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
							    <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>
			
			<div class="btn-group-sm" id="toolbar" role="group">
				<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="business:serviceItem:add">
	                <i class="fa fa-plus"></i> 添加
	            </a>
				<a class="btn btn-info disabled auditBtn" onclick="startAudit()"   shiro:hasPermission="business:serviceItem:add">
					<i class="fa fa-plus"></i> 发起审核
				</a>
	        </div>


	        <div class="col-sm-12 select-table table-striped">
			    <table id="bootstrap-table"></table>
			</div>
		</div>
	</div>
	<th:block th:include="common/include :: footer" />

	<script th:inline="javascript">
		var editFlag = '';
		var saleOnFlag = '';
		var saleOffFlag = '';

		var prefix = ctx + "/business/serviceItem";
		var carDatas = [[${@dict.getType('si_car_package')}]];
		var catalogDatas = [[${@dict.getType('si_service_catalog')}]];
		var auditDatas = [[${@dict.getType('si_audit_status')}]];
		var saleDatas = [[${@dict.getType('si_sale_status')}]];
		$(function() {
		    var options = {
		        url: prefix + "/query",
		        createUrl: prefix + "/addPage",
		        updateUrl: prefix + "/editPage?id={id}",
		        removeUrl: prefix + "/remove",
		        modalName: "服务单项",
		        columns: [{
		            checkbox: true
		        },
				{
					field: 'id',
					title: '',
					visible: false
				},
		        {
		            field: 'name',
		            title: '服务项名称',
		        },
		        {
		            field: 'originalPrice',
		            title: '服务项原价',
		        },

		        {
		            field: 'discountPrice',
		            title: '服务项折扣价',
		        },
				{
					field: 'carPackage',
					title: '是否套餐',
					align: 'center',
					formatter: function(value, row, index) {
						console.log(value, carDatas);
						return $.table.selectDictLabel(carDatas, value);
					}
				},
				{
					field: 'info',
					title: '备注信息',
				},
				{
					field: 'serviceCatalog',
					title: '服务分类',
					align: 'center',
					formatter: function(value, row, index) {
						console.log(value, catalogDatas);
						return $.table.selectDictLabel(catalogDatas, value);
					}
				},
				{
					field: 'auditStatus',
					title: '审核状态',
					align: 'center',
					formatter: function(value, row, index) {
						console.log(value, auditDatas);//索引：数据自典明细对象
						return $.table.selectDictLabel(auditDatas, value);
					}
				},
				{
					field: 'saleStatus',
					title: '上架状态',
					align: 'center',
					formatter: function(value, row, index) {
						console.log(value, saleDatas);
						return $.table.selectDictLabel(saleDatas, value);
					}
				},
		        {
		            title: '操作',
		            align: 'center',
		            formatter: function(value, row, index) {
		            	//把全局变量赋值给局部变量，使用局部变量进行相关操作
		            	var editFlagTemp = editFlag;
		            	var saleOnFlagTemp = saleOnFlag;
						var saleOffFlagTemp = saleOffFlag;
		                var actions = [];
		                if(row.saleStatus == 1){
		                	//如果是上架状态，是不能进行编辑的
							editFlagTemp += " disabled";
						}else {
		                	//如果是套餐，且处于审核状态中是不能进行编辑的
		                	if(row.carPackage == 1 && row.auditStatus == 1){
								editFlagTemp += " disabled";
							}

						}
		                actions.push('<a class="btn btn-success btn-xs ' + editFlagTemp + '" href="javascript:void(0)" ' +
								'onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
						//销售状态为上架的情况显示下架按钮
		                if(row.saleStatus == 1){
							actions.push('<a class="btn btn-danger btn-xs ' + saleOffFlagTemp + '" href="javascript:void(0)" ' +
									'onclick="saleOffOp(\'' + row.id + '\')"><i class="fa fa-edit"></i>下架</a> ');
						}else{
							//销售状态为下架的情况显示上架按钮，如果是套餐而且还没有审核通过还需要把按钮置为不可点击
		                	if(row.carPackage == 1 && row.auditStatus != 2){
								saleOnFlagTemp += " disabled";
							}
							actions.push('<a class="btn btn-info btn-xs' + saleOnFlagTemp + '" href="javascript:void(0)" ' +
									'onclick="saleOnOp(\'' + row.id + '\')"><i class="fa fa-edit"></i>上架</a> ');
						}
		               return actions.join('');
		            }
		        }]
		    };
		    // 用于初始化整个table列表
		    $.table.init(options);
			$("#bootstrap-table").on("check.bs.table uncheck.bs.table check-all.bs.table uncheck-all.bs.table load-success.bs.table", function () {
				//下架才能进行编辑
				var rows = $.table.selectColumns("carPackage");
				//是套餐
				if(rows.length==1 && rows[0]==1){
					var auditStatus = $.table.selectColumns("auditStatus")[0];
					console.log(auditStatus==0);
					//审核状态是初始化或重新发起才将发起审核的按钮的disabled进行移除
					if(auditStatus==0 || auditStatus==3){
						$(".auditBtn").removeClass("disabled");
						return;
					}
				}
				$(".auditBtn").addClass("disabled");
			});
		});

		function saleOnOp(id) {
			$.modal.confirm("确认需要上架此服务项吗?", function() {
				$.operate.submit(prefix + "/saleOn?id="+id, "post");
			});
		}

		function saleOffOp(id){
			$.modal.confirm("确认需要下架此服务项吗?", function() {
				$.operate.submit(prefix + "/saleOff?id="+id, "post");
			});
		}

		function startAudit() {
			//获取选中的复选框中对应的这条数据的id
			var rowIds = $.table.selectColumns("id");
			console.log(rowIds);
			$.modal.open("发起审核", prefix + "/auditPage?id="+rowIds[0]);
		}
	</script>
</body>
</html>